package com.paradogs.framework.utils;

import com.paradogs.common.utils.ProtoUtils;
import com.paradogs.framework.server.MessageHeader;
import com.paradogs.framework.server.PRMessage;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: yumi
 * @date: 2023/12/26  14:54
 * @Description: cg-gc 消息发送工具
 */
@Slf4j
public class MsgUtils {

    public static final String ROUTE_GC = "_net.gc";
    public static final String ROUTE_ERR = "_net.err";

    /** 通知客户端，目前仅支持本服通知 */
    public static <T> void notifyClient(Channel channel, String route, T data, String channelId, Long cgId) {
        PRMessage msg = new PRMessage();
        MessageHeader header = new MessageHeader();
//        header.setServer("c");
        header.setReplyId(cgId); // 回复的消息 ID
        header.setRoute(route);
        header.setChannelId(channelId);
        msg.setHeader(header);
        msg.setData(ProtoUtils.encode(data));
        channel.writeAndFlush(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(ProtoUtils.encode(msg)))); // 发送
        log.debug("gc: {}, {}", header, data);
    }

}
